﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using MicropleSoft.WmsApp.Base.Window;
using MicropleSoft.WmsApp.Library;
using MicropleSoft.WmsApp.Library.Factory;
using MicropleSoft.WmsApp.Library.ServiceFacade;

namespace MicropleSoft.WmsApp.BaseDataManage
{
    public partial class FormPackManage : BaseForm
    {
        public FormPackManage()
        {
            InitializeComponent();
            this.gridView1.CustomRowCellEdit += GridViewMainCustomRowCellEdit;
        }

        private void GridViewMainCustomRowCellEdit(object sender, DevExpress.XtraGrid.Views.Grid.CustomRowCellEditEventArgs e)
        {
            if (e.Column == this.gridColumn1)
            {
                var user = this.gridView1.GetRow(e.RowHandle) as Pack;
                if (user != null)
                {
                    if (user.DomainObjectState != DomainEntityState.Added)
                    {
                        e.RepositoryItem = base.RepTextEditReadOnly;
                    }
                }
            }
        }

        private void CmdQuery_Click(object sender, EventArgs e)
        {
            try
            {
                var list = base.ServerProxy.Query<Pack>("GetPackList", " and 1=1 ");
                this.xGridControl1.DataSource = list;
            }
            catch (Exception exception)
            {
                Base.Exceptions.ExceptionHandler.ShowException(exception);
            }

        }

        private void CmdAdd_Click(object sender, EventArgs e)
        {
            var pack = DomainObjectFactory.CreatePack();
            if (this.xGridControl1.DataSource == null)
            {
                this.xGridControl1.DataSource = new List<Pack>();
            }
            ((List<Pack>)this.xGridControl1.DataSource).Add(pack);
            this.xGridControl1.RefreshDataSource();

            for (int idx = 0; idx < ((List<Pack>)this.gridView1.DataSource).Count; idx++)
            {
                var row = (Pack)this.gridView1.GetRow(idx);

                if (row == pack)
                {
                    this.gridView1.FocusedRowHandle = idx;
                }
            }
        }

        private void CmdSave_Click(object sender, EventArgs e)
        {
            try
            {
                //采用某些字段不更新的方法
                ServerProxy.Save<Pack>("SavePacks", (List<Pack>)this.xGridControl1.DataSource);
            }
            catch (Exception exception)
            {
                Base.Exceptions.ExceptionHandler.ShowException(exception);
            }
        }

        private void CmdDetail_Click(object sender, EventArgs e)
        {
            try
            {
                IBaseDomainObject row = (Pack) this.gridView1.GetRow(this.gridView1.FocusedRowHandle);
                if (row == null)
                    return;

                var form = new FormPackManageEditor(ref row);
                
                form.ShowDialog(this);
            }
            catch (Exception exception)
            {
                Base.Exceptions.ExceptionHandler.ShowException(exception);
            }
        
        }
    }
}